#!/bin/sh
#
# Copyright (C) 2000-2023 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#

#
# Create the sed file for configuring everything
#
cwd=`pwd`
. ${cwd}/scripts/functions
mkdir -p ${bin}
mkdir -p ${tmp}
out="${tmp}/sed_tmp"

# set ports and other default parameters
BASEPORT=${BASEPORT:-8101}
AUTOCHANGER_SCRIPT=${AUTOCHANGER_SCRIPT:-mtx-changer}
REMOTE_HOST_ADDR=${REMOTE_HOST_ADDR:-localhost}
REMOTE_HOST_NAME=${REMOTE_HOST_NAME:-localhost}
LIBDBI=${LIBDBI:-}
libdbi=${LIBDBI}
srcdir=${BACULA_SOURCE}
dirport=${BASEPORT}
fdport=`expr ${BASEPORT} '+' 1`
sdport=`expr ${BASEPORT} '+' 2`
db_name=${db_name:-"regress"}
db_user=${db_user:-"regress"}
db_password=${db_password:-""}
os=`uname -s`
# Drop Linux or FreeBSD from OS to shorten name
if test ${os} = "Linux" -o ${os} = "FreeBSD"; then
  os=`./scripts/get-os`
else
  os=${os}-`./scripts/get-os`
fi
dbengine=`echo ${WHICHDB} | sed -e 's/--with-//' -e 's/=.*//'`
bversion=`sed -n -e 's/^#define VERSION.*"\(.*\)"$/\1/p' ${srcdir}/src/version.h`
bdate=`sed -n -e 's/^#define LSMDATE.*"\(.*\)"$/\1/p' ${srcdir}/src/version.h`
builddir="${src}"
win32_wsep_file=`echo $WIN32_FILE | sed -e 's=/=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\=g'`

# Create sed command script
echo "s%@remotehostaddr@%${REMOTE_HOST_ADDR}%g" >${out}
echo "s%@remotedirname@%${REMOTE_DIR_NAME}%g" >>${out}
echo "s%@sbindir@%${bin}%g" >>${out}
echo "s%@libdir@%${bin}%g" >>${out}
echo "s%@sysconfdir@%${bin}%g" >>${out}
echo "s%@sd_user@%`whoami`%g" >>${out}
echo "s%@sd_group@%`id -g -n`%g" >>${out}
echo "s%@scriptdir@%${scripts}%g" >>${out}
echo "s%@working_dir@%${working}%g" >>${out}
echo "s%@piddir@%${working}%g" >>${out}
echo "s%@subsysdir@%${working}%g" >>${out}
echo "s%@job_email@%${EMAIL}%g" >>${out}
echo "s%@tape_drive@%${TAPE_DRIVE}%g" >>${out}
echo "s%@tape_control@%${TAPE_CONTROL}%g" >>${out}
echo "s%@autochanger@%${AUTOCHANGER}%g" >>${out}
echo "s%@changer_script@%${AUTOCHANGER_SCRIPT}%g" >>${out}
echo "s%@tmpdir@%${tmp}%g" >>${out}
echo "s%@hostname@%${HOST}%g" >>${out}
echo "s%@changer_path@%${AUTOCHANGER_PATH}%g" >>${out}
echo "s%@tape_drive1@%${TAPE_DRIVE1}%g" >>${out}
echo "s%@tape_control1@%${TAPE_CONTROL1}%g" >>${out}
echo "s%@smtp_host@%${SMTP_HOST}%g" >>${out}
echo "s%@disk_drive@%${tmp}/disk-changer%g" >>${out}
echo "s%@hostname@%${hostname}%g" >>${out}
echo "s%@hostname1@%${hostname1}%g" >>${out}
echo "s%@hostname2@%${hostname2}%g" >>${out}
echo "s%@hostname3@%${hostname3}%g" >>${out}
echo "s%@hostname1_files@%${hostname1_files}%g" >>${out}
echo "s%@hostname_files@%${hostname_files}%g" >>${out}
echo "s%@hostname2_files@%${hostname2_files}%g" >>${out}
echo "s%@hostname3_files@%${hostname3_files}%g" >>${out}
echo "s%@hostname1_password@%${hostname1_password}%g" >>${out}
echo "s%@hostname2_password@%${hostname2_password}%g" >>${out}
echo "s%@hostname3_password@%${hostname3_password}%g" >>${out}
echo "s%@dirport@%${dirport}%g" >>${out}
echo "s%@sdport@%${sdport}%g" >>${out}
echo "s%@fdport@%${fdport}%g" >>${out}
echo "s%@regressdir@%${cwd}%g" >>${out}
echo "s%@dbengine@%${dbengine}%g" >>${out}
echo "s%@srcdir@%${srcdir}%g" >>${out}
echo "s%@sitename@%${SITE_NAME}%g" >>${out}
echo "s%@os@%${os}%g" >>${out}
echo "s%@bversion@%${bversion}%g" >>${out}
echo "s%@bdate@%${bdate}%g" >>${out}
echo "s%@libdbi@%${libdbi}%g" >>${out}
echo "s%@win32_client@%${WIN32_CLIENT}%g" >>${out}
echo "s%@win32_addr@%${WIN32_ADDR}%g" >>${out}
echo "s%@win32_store_addr@%${WIN32_STORE_ADDR}%g" >>${out}
echo "s%@win32_file@%${WIN32_FILE}%g" >>${out}
echo "s%@win32_wsep_file@%${win32_wsep_file}%g" >>${out}
echo "s%@win32_port@%${WIN32_PORT}%g" >>${out}
echo "s%@win32_password@%${WIN32_PASSWORD}%g" >>${out}
echo "s%@win32_hv_vm@%${WIN32_HV_VM}%g" >>${out}
echo "s%@win32_hv_working_dir@%${WIN32_HV_WORKING_DIR}%g" >>${out}
echo "s%@win32_sp_site@%${WIN32_SP_SITE}%g" >>${out}
echo "s%@win32_sp_working_dir@%${WIN32_SP_WORKING_DIR}%g" >>${out}
echo "s%@db_name@%${db_name}%g" >>${out}
echo "s%@db_user@%${db_user}%g" >>${out}
echo "s%@db_password@%${db_password}%g" >>${out}
echo "s%@builddir@%${builddir}%g" >>${out}
echo "s%@kvm_host1@%${KVM_HOST1}%g" >> ${out}
echo "s%@kvm_host2@%${KVM_HOST2}%g" >> ${out}
echo "s%@st_auth@%${ST_AUTH}%g" >>${out}
echo "s%@st_user@%${ST_USER}%g" >>${out}
echo "s%@st_key@%${ST_KEY}%g" >>${out}
echo "s%@plugdockerhost@%${PLUGDOCKERHOST}%g" >>${out}
echo "s%@k8spwd@%${cwd}%g" >>${out}
